package com.back.Controller;

import com.back.Dao.Device;
import com.back.Dao.R;
import com.back.Service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    // 测试成功
    @GetMapping("/device")
    public R findAllDeice()
    {
        List<Device> devices = deviceService.findAllDeice();
        if(devices.size() > 0)
            return R.success(devices);
        else
            return R.error("未查询到信息");
    }

    @GetMapping("/device/{id}")
    public R findDeviceById(@PathVariable Integer id) {
        Device device = deviceService.findDeviceById(id);
        if(device != null) {
            return R.success(device);
        }
        else
            return R.error("未查询到此id");


    }

    @GetMapping("/deviceAdd/{address}")
    public R findDeviceByAddress(@PathVariable String address) {
        List<Device> devices = deviceService.findDeviceByAddress(address);
        return R.success(devices);
    }

    @DeleteMapping("/device/{id}")
    public R DelDevice(@PathVariable int id) {
        Integer result = deviceService.DelDevice(id);
        if(result == 0)
            return R.error("删除失败，请勿重复删除");
        return R.success(result);
    }

    @PostMapping("/device")
    public R addDevice(@RequestBody Device device) {
        if(device.getName().equals("") || device.getAddress().equals("") || device.getLng().equals("") || device.getLat().equals("") || device.getDtuid().equals(""))
            return R.error("插入失败");
        deviceService.addDevice(device);
        return R.success("插入成功");
    }

    @PostMapping("/updatedevice")
    public R updateDevice(@RequestBody Device device) {
        deviceService.updateDevice(device);
        return R.success("修改成功");
    }

}
